<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"
    />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script src="./js/lib.js"></script>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="./vendor/wangEditor-3.1.1/release/wangEditor.min.js"></script>
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/spark-md5.min.js"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="">新闻管理</a>
            <a href="">新闻管理</a>
            <a>
                <cite>添加新闻</cite>
            </a>
        </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);"
            title="刷新">
            <i class="layui-icon" style="line-height:30px">ဂ</i>
        </a>
    </div>
    <div class="x-body">
        <div class="layui-form layui-form-pane">
            <div class="layui-form-item">
                <label for="" class="layui-form-label">
                    <span class="x-red">*</span>标题-简体
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="titleCn" required lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <label for="" class="layui-form-label">
                    <span class="x-red">*</span>标题-繁体
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="titleTw" required lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <label for="" class="layui-form-label">
                    <span class="x-red">*</span>标题-英文
                </label>
                <div class="layui-input-inline">
                    <input type="text" name="titleEn" required lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <label for="" class="layui-form-label">
                    <span class="x-red">*</span>状态
                </label>
                <div class="layui-input-inline">
                    <select name="status" id="">
                        <option value="0">不发布</option>
                        <option value="1">发布</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">
                    文章摘要(多个以;分隔)
                </label>
                <div class="layui-input-inline" style="width:60%">
                    <input type="text" name="tags" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">
                    轮播图 1
                </label>
                <div class="layui-input-inline" style="width:60%">
                    <input type="text" name="banner1"  autocomplete="off" class="layui-input">
                </div>
                <div class="position-box">
                    <a class="layui-btn layui-btn-normal">添加</a>
                    <input type="file" data-op="banner1" class="hidden-upload-btn">
                </div>
                <a class="layui-btn" onclick="checkImg('banner1')">检查</a>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">
                    轮播图 2
                </label>               
                <div class="layui-input-inline" style="width:60%">
                    <input type="text" name="banner2"  autocomplete="off" class="layui-input">
                </div>
                <div class="position-box">
                    <a class="layui-btn layui-btn-normal">添加</a>
                    <input type="file" data-op="banner2" class="hidden-upload-btn">
                </div>
                <a class="layui-btn" onclick="checkImg('banner2')">检查</a>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">
                    轮播图 3
                </label>
                <div class="layui-input-inline" style="width:60%">
                    <input type="text" name="banner3" autocomplete="off" class="layui-input">
                </div>
                <div class="position-box">
                    <a class="layui-btn layui-btn-normal">添加</a>
                    <input type="file" data-op="banner3" class="hidden-upload-btn">
                </div>
                <a class="layui-btn" onclick="checkImg('banner3')">检查</a>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">
                    轮播图 4
                </label>
                <div class="layui-input-inline" style="width:60%">
                    <input type="text" name="banner4"  autocomplete="off" class="layui-input">
                </div>
                <div class="position-box">
                    <a class="layui-btn layui-btn-normal">添加</a>
                    <input type="file" data-op="banner4" class="hidden-upload-btn">
                </div>
                <a class="layui-btn" onclick="checkImg('banner4')">检查</a>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">
                    轮播图 5
                </label>
                <div class="layui-input-inline" style="width:60%">
                    <input type="text" name="banner5"  autocomplete="off" class="layui-input">
                </div>
                <div class="position-box">
                    <a class="layui-btn layui-btn-normal">添加</a>
                    <input type="file" data-op="banner5" class="hidden-upload-btn">
                </div>
                <a class="layui-btn" onclick="checkImg('banner5')">检查</a>
            </div>
            
            <div class="layui-form-item layui-form-text">
                <label for="" class="layui-form-label"><span class="x-red">*</span>内容-简体</label>
                <div class="layui-input-block">
                    <div id="htmlCn" name="htmlCn"></div>
                </div>
                <label for="" class="layui-form-label"><span class="x-red">*</span>内容-繁体</label>
                <div class="layui-input-block">
                    <div id="htmlTw" name="htmlTw"></div>
                </div>
                <label for="" class="layui-form-label"><span class="x-red">*</span>内容-英文</label>
                <div class="layui-input-block">
                    <div id="htmlEn" name="htmlEn"></div>
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-filter="newAdd" lay-submit>
                    确定新增
                </button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </div>

    <script>
        var aId = Cookies.get('kplus-helmetAdminId');
        var aToken = Cookies.get('kplus-helmetAdminToken');
        layui.use(['form', 'layer','layedit'], function () {
            var layer = layui.layer,
                layedit = layui.layedit,
                form = layui.form;

            var WEditor = window.wangEditor,
                editorCn = new WEditor('#htmlCn'),
                editorTw = new WEditor('#htmlTw'),
                editorEn = new WEditor('#htmlEn');
            editorCn.create();
            editorTw.create();
            editorEn.create();
            //图片上传
            $('input.hidden-upload-btn').on('change', function () {
                var file = $(this)[0].files[0],         // 获取文件对象
                    fileName = file.name,                //文件名
                    fizeSize = file.size,                //文件大小
                    fileExt = fileName.split('.')[1],   //文件扩展名 
                    fileType = file.type,               // 文件类型
                    _this = $(this);                    //操作对象
                //判断文件类型
                if (typeof (file) == 'undefined' || file.size <= 0) {
                    layer.msg('图片文件错误，请重新上传!', { time: 4000 })
                    $(this).file[0].value = '';
                    return false;
                }
                fileReader = new FileReader();
                fileReader.onload = function (e) {
                    var hexHash = SparkMD5.hash(e.target.result);
                    //console.log(hexHash)
                    var formfile = new FormData();  //穿件formData 对象
                    formfile.append('file', file)   //文件对象
                    formfile.append('fileName', fileName)
                    formfile.append('fileExt', fileExt)
                    formfile.append('hexHash', hexHash)
                    formfile.append('aId', aId)
                    formfile.append('aToken', aToken)
                    formfile.append('storage', 'kplus-helmet/web/news/IMG/')  //存储地址
                    //上传文件
                    updateFile(formfile, _this)
                }
                fileReader.readAsDataURL(file) //读取文件 
            })
            //上传文件函数
            function updateFile(form_data, obj) {
                var loadIndex1 = layer.load(1, { shade: [0.5, '#000'] });
                $.ajax({
                    url: bAsEuRl + uRlS.upload,
                    type: 'post',
                    data: form_data,
                    processData: false,
                    contentType: false,
                    async: false,
                    success: function (data) {
                        if (data.msg.code == 200) {
                            //console.log(data)
                            console.log(obj.attr('data-op'))
                            $('input[name="' + obj.attr('data-op') + '"]').val(data.data.src)
                            layer.close(loadIndex1)
                        } else {
                            layer.msg(data.msg.msg, { time: 4000 })
                            layer.close(loadIndex1)
                        }
                    }
                })
            }
                
            form.on('submit(newAdd)', function (data) { 
                console.log(data)
                console.log(editorCn.txt.html())
                console.log(editorTw.txt.html())
                console.log(editorEn.txt.html())
                var newsData = {
                    'aId':aId,
                    'aToken': aToken,
                    'img': data.field.banner1 + '|' + data.field.banner2 + '|' + data.field.banner3 + '|' + data.field.banner4 + '|' + data.field.banner5,
                    'title': 'cn:' + data.field.titleCn + '|tw:' + data.field.titleTw + '|en:' + data.field.titleEn,
                    'tags': data.field.tags,
                    'cn':editorCn.txt.html(),
                    'tw': editorTw.txt.html(),
                    'en': editorEn.txt.html(),
                    'status': data.field.status
                }
                addNews(newsData)
                return false;
             })
             // 新增新闻
            function addNews(data) { 
                var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
                $.ajax({
                    url: 'bAsEuRl' + uRlS.news,
                    type: 'put',
                    data: data,
                    async: false,
                    success: function (data) {
                        if (data.msg.code == 200) {
                            //console.log(data)
                            layer.close(loadIndex)
                            layer.msg(data.data.msg, {time:2000});
                            window.parent.location.reload();
                            parent.layer.close(layer.index);
                        } else {
                            layer.msg(data.msg.msg, { time: 4000 })
                            layer.close(loadIndex)
                        }
                    },
                    error: function (param) {
                        layer.close(loadIndex)
                        layer.msg('发生严重错误，请联系管理员修复！', { time: 2000 });
                    }
                })
            }
             



        })


    </script>
</body>

</html>